package hashmap;

import java.util.HashSet;

public class LeetCode_202 {
    public static void main(String[] args) {
        LeetCode_202 leet = new LeetCode_202();
        int count = leet.count(19);
        System.out.println(leet.isHappy(19));
        System.out.println(count);
    }
    public int count(int n){
        int g = n % 10;
        int s = n / 10;
        int result = 0;
        while(s > 0){
            result += (g * g);
            n = s;
            g = n % 10;
            s = n / 10;
        }
        result += (g * g);
        return result;
    }
    public boolean isHappy(int n) {
        HashSet<Integer> integers = new HashSet<>();
        while (true){
            int count = count(n);
            if(count == 1){
                break;
            }
            if(integers.contains(count)){
                return false;
            }
            integers.add(count);
            n = count;
        }
        return true;
    }
}
